package application.client.gui;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.List;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import middleware.datacontainer.searchmedium.MediumContainer;
import application.client.gui.components.ImagePanel;
import application.client.gui.components.LoginComponents;
import application.controller.client.GuiController;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class MainFrame extends javax.swing.JFrame {

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	private JPanel searchPanel;
	private JPanel dummyPanelR;
	private JPanel dummyPanelL;
	private JButton showResultButton;
	private JComboBox searchCombo;
	private JButton searchButton;
	private JPanel searchFunctionPanel;
	private JTabbedPane tabPane;
	private JScrollPane resultScrollPane;
	private JTable resultTable;
	private JPanel resultPanel;
	private JTextField searchField;
	private String[][] searchResult =new String[][] { };
	private String[] resultTableColumnHeader =new String[] { "ID", "MediumType","Title","Description","Year","Language","Department" };
	private int selectedRowIndexResultTable;
	
	private HashMap<Integer,MediumContainer> mediums=new HashMap<Integer,MediumContainer>();
	

	public MainFrame() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			
				{
					tabPane = new JTabbedPane();
					getContentPane().add(tabPane, BorderLayout.CENTER);
					tabPane.setPreferredSize(new java.awt.Dimension(794, 472));
					tabPane.setMaximumSize(new java.awt.Dimension(700, 260));
					{
						searchPanel = new JPanel(true);
						tabPane.addTab("searchPanel", null, searchPanel, null);
						BorderLayout mainPanelLayout = new BorderLayout();
						searchPanel.setLayout(mainPanelLayout);
						searchPanel.setBackground(new java.awt.Color(255,255,255));
						searchPanel.setAutoscrolls(true);
						{
							resultPanel = new JPanel();
							BorderLayout resultPanelLayout = new BorderLayout();
							resultPanel.setLayout(resultPanelLayout);
							searchPanel.add(resultPanel, BorderLayout.CENTER);
							resultPanel.setPreferredSize(new java.awt.Dimension(759, 290));
							resultPanel.setBackground(new java.awt.Color(255,255,255));
							resultPanel.setBounds(0, 0, 0, 0);
							resultPanel.setSize(800, 420);
							{
								resultScrollPane = new JScrollPane();
								resultPanel.add(resultScrollPane, BorderLayout.NORTH);
								resultScrollPane.setPreferredSize(new java.awt.Dimension(700, 260));
								resultScrollPane.setMaximumSize(new java.awt.Dimension(700, 260));
								resultScrollPane.setSize(700, 260);
								{
									TableModel resultTableModel = 
											new DefaultTableModel(
													searchResult ,
													resultTableColumnHeader);
									resultTable = new JTable();
									resultScrollPane.setViewportView(resultTable);
									resultTable.setModel(resultTableModel);
									resultTable.setLayout(null);
									resultTable.setPreferredSize(new java.awt.Dimension(700, 240));
									resultTable.setSize(700, 240);
									resultTable.setIntercellSpacing(new java.awt.Dimension(1, 1));
									resultTable.setShowGrid(true);
									resultTable.getTableHeader().setEnabled(true);
									
								}
							}
							{
								showResultButton = new JButton();
								resultPanel.add(showResultButton, BorderLayout.EAST);
								showResultButton.setLayout(null);
								showResultButton.setText("show Medium");
								showResultButton.setSize(100, 10);
								showResultButton.setPreferredSize(new java.awt.Dimension(100, 10));
								showResultButton.addActionListener(new ActionListener(){

									@Override
									public void actionPerformed(ActionEvent e) {
										if(resultTable.getModel().getRowCount()>0)
										{
											GuiController.getInstance().addMediumTab(mediums.get(resultTable.getSelectedRow()));
										}
									}									
								});
								
							}
						}						
						{
							searchFunctionPanel = new JPanel();
							GroupLayout searchPanelLayout = new GroupLayout((JComponent)searchFunctionPanel);
							searchFunctionPanel.setLayout(searchPanelLayout);
							searchPanel.add(searchFunctionPanel, BorderLayout.NORTH);
							searchFunctionPanel.setPreferredSize(new java.awt.Dimension(420, 55));
							searchFunctionPanel.setBackground(new java.awt.Color(255,255,255));						
							searchFunctionPanel.setBounds(0, 0, 0, 0);
							{
								searchField = new JTextField();
								searchField.setVisible(true);
								searchField.setSize(210, 23);
								
							}
							{
								ComboBoxModel searchComboModel = 
										new DefaultComboBoxModel(
												new String[] { "Alles", "Fachbereich","Person","InventarNummer" });
								searchCombo = new JComboBox();
								searchCombo.setModel(searchComboModel);	
								searchCombo.setPreferredSize(new java.awt.Dimension(50,23));
							}
							{							
								searchButton = new JButton();
								searchButton.setVisible(true);
								searchButton.setText("Go!");
								searchButton.setPreferredSize(new java.awt.Dimension(10,25));
								searchButton.addActionListener(new ActionListener()
								{
									
									@Override
									public void actionPerformed(ActionEvent e) {
										Object source=e.getSource();
										if(searchField.getText()!="")
										{
											GuiController.getInstance().searchMedium(searchField.getText(), searchCombo.getSelectedItem().toString());
										}									
									}
									
								});
							}
							
							searchPanelLayout.setHorizontalGroup(searchPanelLayout.createSequentialGroup()
									.addContainerGap(393, 393)
									.addComponent(searchField, GroupLayout.PREFERRED_SIZE, 211, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
									.addComponent(searchCombo, GroupLayout.PREFERRED_SIZE, 105, GroupLayout.PREFERRED_SIZE)
									.addGap(0, 14, Short.MAX_VALUE)
									.addComponent(searchButton, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)
									.addContainerGap());
							searchPanelLayout.setVerticalGroup(searchPanelLayout.createSequentialGroup()
									.addContainerGap()
									.addGroup(searchPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
											.addComponent(searchField, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
											.addComponent(searchCombo, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
											.addComponent(searchButton, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
											.addContainerGap(21, 21));
						}
						{
							ImagePanel imagePanel = new ImagePanel();
							searchPanel.add(imagePanel, BorderLayout.SOUTH);
							{
									imagePanel.add(new LoginComponents());
							}
						}
						{
							dummyPanelR = new JPanel();
							searchPanel.add(dummyPanelR, BorderLayout.EAST);
							dummyPanelR.setBackground(new java.awt.Color(255,255,255));
							dummyPanelR.setSize(10, 400);
							dummyPanelR.setPreferredSize(new java.awt.Dimension(10, 393));
						}
						{
							dummyPanelL = new JPanel();
							searchPanel.add(dummyPanelL, BorderLayout.WEST);
							dummyPanelL.setBackground(new java.awt.Color(255,255,255));
						}						
					}
				setDefaultCloseOperation(EXIT_ON_CLOSE);
			}
			this.setSize(800, 500);
		 
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void setResultToResultList(List<MediumContainer> result)
	{
		searchResult = new String[result.size()][resultTableColumnHeader.length-1];
		TableModel resultTableModel=new DefaultTableModel(searchResult, resultTableColumnHeader);
				
		int row = 0;
		for (MediumContainer medium : result) {
			if(medium != null)
			{
				resultTableModel.setValueAt(medium.getId(), row, 0);
				resultTableModel.setValueAt(medium.getType(), row, 1);
				resultTableModel.setValueAt(medium.getTitle(), row, 2);
				resultTableModel.setValueAt(medium.getDescription(), row, 3);
				resultTableModel.setValueAt(medium.getYear(), row, 4);//Jahr
				resultTableModel.setValueAt(medium.getLanguage(), row, 5);//Sprache
				resultTableModel.setValueAt(medium.getDepartment(), row, 6);//Fachbereich
				
				mediums.put(row, medium);
				row++;
			}
		}
		resultTable.setModel(resultTableModel);
		resultTable.getSelectionModel().addListSelectionListener(new ListSelectionListener(){

			@Override
			public void valueChanged(ListSelectionEvent e) {
				  if (e.getValueIsAdjusting()) {
				        return;
				  }
				  selectedRowIndexResultTable=resultTable.getSelectedRow();	 
				  
			}
		
		});
		
	}
	public JTabbedPane getTabPane()
	{
		return tabPane;
	}
	
}
	
